// pages/myOrder/myOrder.js
import { regeneratorRuntime } from '../../utils/runtime.js'

import { getOrder } from '../../service/public.js'

Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		titles: ['全部订单', '待付款', '待收货', '评价'],
		filterOrders: [],
		active: 0,
	},
	orders: [],
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		console.log()
		const type = parseInt(options.type)
		this.changeIndex(type)

		// 获得openid
		const openid = wx.getStorageSync('openid') || null

		this._getOrder(openid)
	},
	async _getOrder(openid) {
		const res = await getOrder({ openid })
		this.orders = res.data.data
    this.filterOrder()
	},
	checkOrder(e) {
		const { state } = e.detail
		const { index } = e.currentTarget.dataset
    this.data.filterOrders[index].state = state

    this.setData({
      filterOrders:this.data.filterOrders
    })
    this.orders.forEach(v=>{
      if (v.state == this.data.filterOrders[index].state) {
        v.state = state
      }
    })
	},
	changeIndex(active) {
		this.setData({
			active,
		})
	},
	filterOrder() {
    let filterOrders 
		if (this.data.active == 0) {
			filterOrders = this.orders
		} else {
			filterOrders = this.orders.filter(v=>v.state == this.data.active)
		}

		this.setData({
			filterOrders
		})
  },
  navChange(e){

    const {index} = e.detail

    this.changeIndex(index)

    this.filterOrder()

  }
})
